<?php


namespace App\Http\Controllers\System;


use App\Http\Controllers\Controller;
use App\Http\Services\System\MenuService;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    //获取菜单路由权限
    public function routes(MenuService $menuService)
    {
        $data = $menuService->getTreeData(false);
        return success("获取路由菜单", $data);
    }

    //菜单树
    public function tree(MenuService $menuService)
    {
        $data = $menuService->getTreeData(true);
        return success("获取菜单树", $data);
    }

    //保存树节点信息
    public function saveNode(Request $request, MenuService $menuService)
    {
        $param = $request->all();
        $data = json_decode($param['data'], true);
        if(!isset($data['id'])){
            return error("数据无效");
        }
        $id = $data['id'];
        unset($data['id']);
        $res = $menuService->saveData($id, $data);
        return $res['code'] === 0 ? success("节点修改成功") : error("节点修改失败", $res['msg']);
    }

    //新增树节点
    public function addMenuNode(Request $request, MenuService $menuService)
    {
        $param = $request->all();
        $res = $menuService->addData($param);
        return $res['code'] === 0 ? success("节点新增成功", $res['data']) : error("节点新增失败", $res['msg']);
    }

    //删除节点
    public function delMenuNode(Request $request, MenuService $menuService)
    {
        $this->validate($request, [
            'id' => 'required'
        ]);
        $id = $request->id;
        $res = $menuService->delData($id);
        return $res['code'] === 0 ? success("节点删除成功") : error("节点删除失败", $res['msg']);
    }
}
